local M = _G.ClientOHOH.class.new(script,_G.ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name)
local ClientOHOH = _G.ClientOHOH
local PVPGui = ClientOHOH.load.UIManager():CreateUI("PVPGui",require(script.PVPGui))
local PVPPopUpGui = ClientOHOH.load.UIManager():CreateUI("PVPPopUpGui",require(script.PVPPopUpGui))
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local Good_Cfg=ClientOHOH.config.Goods
local Rank_Cfg=ClientOHOH.config.Rank
local EventList = ClientOHOH.load.EventList();
local FunctionList = ClientOHOH.load.FunctionList();
local ConnectClass = _G.CommonOHOH.load.Connect()
local PVPEvent = ConnectClass.new()
local LanguageHelper=_G.LanguageHelper 
local Tool=require(script.tool)
-- local PVPTool=_G.CommonOHOH.load.PVPTool();

function M.new(self,...)
    return M(self,...)
end
-- local pvpData = {
--     [1] = {
--         userId = 7267525766,
--         -- damage = "100k",
--         damage = 1000000,
--         pvpEvent = PVPEvent
--     },
--     [2] = {
--         userId = 7267525766,
--         -- damage = "200k",
--         damage = 2000000,
--         pvpEvent = PVPEvent
--     }
-- }

-- local myPVPData={
--     score = "63471",
--     rank = "青铜II",
--     icon = "rbxassetid://17839557251",
--     rankLevel = 3
-- }

-- local defenseData = {
--     [1] = {
--         name = "eeeeeeee",
--         headIcon = "rbxassetid://17573031626",
--         rank = "青铜I",
--         rankIcon = "rbxassetid://17839557251",
--         isWin = false,
--         pieceNumStr = "-200",
--         timeStr = "07/01/2024 12:07 PM",
--         rankLevel = 1
--     },
--     [2] = {
--         name = "fffffffff",
--         headIcon = "rbxassetid://13695379732",
--         rank = "青铜IV",
--         rankIcon = "rbxassetid://17839557251",
--         isWin = true,
--         pieceNumStr = "+500",
--         timeStr = "03/05/2024 09:07 PM",
--         rankLevel = 2
--     }
-- }


function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.DataStore = self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    self.player = parent
    -- self.isPKing = false
    self.cdTime = 3 --单位s
    local score =  self.DataStore:GetData("base").elo
    -- local myPVPData =  Tool.GetInfo(score)
    local PVPData = self.DataStore:GetData("PVPData")

    --获取数据库的匹配数据
   local matchData=  self:Invoke(FunctionList.get_match_data,score)

    CallBackManager:AddRbxConnect(self,PVPGui.pkEndEvent, function()
       
       local rivalId =PVPGui:GetRivalId()
        self:Invoke(FunctionList.gen_record, rivalId)
        
    end);
    CallBackManager:AddRbxConnect(self,PVPGui.PKListRenewEvent, function()
       
        local myScore =PVPGui:GetMyScore()
      
        local matchData=  self:Invoke(FunctionList.get_match_data,myScore)
        PVPGui:FullPvPData(matchData)
     end);
     
    PVPGui:FullMyData(score)
    PVPGui:FullPvPData(matchData);
    PVPGui:FullDefenseData(PVPData);
    PVPPopUpGui:Full(self:GetRankData())
    self.UserDataEventConnect=self.parent.UserDataEvent:Connect(function(data)
        -- 调用顺序不可更改
		if data["base"] then
            PVPGui:FullMyData(data["base"].elo)
		end
        if data["PVPData"] then
            PVPGui:FullDefenseData(data["PVPData"])
		end
	end)
    self:CountDown()
    self:AddConnect()

    
end

function M.AddConnect(self)
    PVPGui.countDownEvent:Connect(function()
        self:CountDown()
    end)
    PVPGui.pkEvent:Connect(function(rivalId)
        if self.player  then
            print("开始PK")
            print("self",self)
            PVPGui:Hide()
            local isWin,myCarData,rivalCarData= self:Invoke(FunctionList.get_pk_result, rivalId)
            print("服务器传回的PK结果 isWIn",isWin,myCarData,rivalCarData)
            self.player:GetComponent(_G.CommonOHOH.load.Common_PVPBattlCtrl()):EnterPvPScene(isWin,rivalId,myCarData,rivalCarData);
        
        else
            print("正在PK中！！！")
        end
    end)
end

--倒计时
function M.CountDown(self)
    local now = self.cdTime
    self:AddProcess(function()
        while now>=0 do
            PVPGui:SetCD(self:TimeToStr(now))
            now = now -1
            self:Wait(1)
        end
        PVPGui:CDIsOver()
    end)
end

--秒转换为00:00
function M.TimeToStr(self,time)
    local minutes = math.floor((time % 3600) / 60)
	local seconds = time - minutes*60
    local timeStr = string.format("%02d:%02d", minutes, seconds)
    return timeStr
end

--读取配置表数据计算出显示需要的数据：积分范围，名字，图片,
function M.GetRankData(self)
    local data = {}
    for index, value in pairs(Rank_Cfg) do
        data[index] = {}
        data[index]["name"] = LanguageHelper:GetText(value.rankName)
        data[index]["icon"] = value.icon
        if index<#Rank_Cfg then
            data[index]["range"] = value.point.."-"..Rank_Cfg[index+1].point
        else
            data[index]["range"] = value.point.."+"
        end
    end
    return data
end


function M.dtor(self)
    M.super.dtor(self)
end


return M 